﻿using SwenAssigment.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SwenAssigment
{
    public partial class AddUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //system
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedIndex == 0)
            {
                GridView1.DataSource = JcUser.GetStaffAsUser(DropDownList1.SelectedIndex, TextBox1.Text);
                GridView1.DataBind();
            }
            else
            {
                Match match = null;
                if (TextBox1.Text != "")
                {
                    if (DropDownList1.SelectedIndex == 1)
                    {
                        match = Regex.Match(TextBox1.Text, @"^[0-9]{1,}$");
                        Label1.Text = "Error, accept only number";
                    }
                    else if (DropDownList1.SelectedIndex == 2)
                    {
                        match = Regex.Match(TextBox1.Text, @"^[a-zA-Z]{4,20}$");
                        Label1.Text = "Error, accept only letter min 4  max 20";
                    }
                    else if (DropDownList1.SelectedIndex == 3)
                    {
                        match = Regex.Match(TextBox1.Text, @"(^[a-zA-Z])+([0-9]{7})+([a-zA-Z]$)");
                        Label1.Text = "NRIC format Error";
                    }
                    if (match.Success)
                    {
                        GridView1.DataSource = JcUser.GetStaffAsUser(DropDownList1.SelectedIndex, TextBox1.Text);
                        GridView1.DataBind();
                        Label1.Visible = false;
                    }
                    else
                    {
                        Label1.Visible = true;
                    }
                }
                else
                {
                    Label1.Visible = true;
                }
            }
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label1.Visible = false;
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextBox3.Text = GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
            TextBox2.Text = GridView1.DataKeys[GridView1.SelectedIndex].Values[1].ToString();
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            ClientScriptManager cs = Page.ClientScript;
            if (TextBox3.Text != "")
            {
                int result = JcUser.CreateUserAccount(Convert.ToInt32(TextBox3.Text), TextBox6.Text, SecureFunction.hashvalue(TextBox4.Text), DropDownList2.SelectedValue);
                if (result > 0)
                {
                    cs.RegisterStartupScript(this.GetType(), "PopupScript", "alert('" + "Account Created" + "');", true);
                }
                else
                {
                    cs.RegisterStartupScript(this.GetType(), "PopupScript", "alert('" + "Account Did not created" + "');", true);
                }
            }
        }
    }
}